/** 创建组件对象 * */
var benchmarkForm = new Form();
var benchmarkTable = new Table();
var benchmarkDialog = new Dialog();
var benchmarkAjax = new Ajax();
var benchmarkColumn = new Column();
var array = [ "benchmarkQueryDiv","benchmarkChartDiv"];

//获取选择的指标的Id
var benchmarkObj = [];

var unitObj = [];
var packageObj = [];


/** 页面加载完执行 * */
$(function() {
	initScript();
	initConditions();
	initDate();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/benchmark/locale/Benchmark_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/benchmark/properties/BenchmarkSurroundProperties_View.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	loadScript(appPath + "/project-yddemo/common/Common.js");
}

/** 初始化查询条件 * */
function initConditions() {
	benchmarkForm.initTableConditions({
		divId : "benchmarkQueryDiv", // 1.归属div*
		type : "table",
		conditions : BenchmarkSurroundProperties_View.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			width : "80", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : benchmarkLocale.text.compare, // 文本*
			action : "yddemo:benchmark:compare",// 增加页面元素控制
			clickFun : function() { // 按钮事件*
				//TODO
				pageQuery();
			}
		}
	});
	var benchmarkInput = $("[key$='indicator']");
	benchmarkInput.bind("click", function() {
		var condition = benchmarkForm.getQueryConditions();
		if(condition.dimension!=""){
			sessionStorage.dimension = condition.dimension;
			benchmarkDialog.iframeDialog(requestPath
					+ "/yddemo/benchmarks/page/unitDialog",
					benchmarkLocale.field.indicator, 900, 500,
					"yddemo:benchmark:indicator", false);
		}else{
			benchmarkDialog.alertDialog(benchmarkLocale.text.noSelectDimension);
		}
	});
	benchmarkInput.bind("focus",function(){
		benchmarkInput.blur();
	});
}

function initDate() {
	$("#benchmarkQueryDiv").find("[key *= 'dataTime']").jeDate({
		isinitVal : true,
		initAddVal : {
			YYYY : "0",
			MM : "0",
			DD : "0"
		},
		// 2.初始化时分秒
		ishmsVal : false,
		format : "YYYY-MM-DD",
		zIndex : 3000,
		language : getJeDateLanguage()
	});
}

/** 分页查询 * */
function pageQuery() {
	//获取时间
	var date = benchmarkForm.getQueryConditions().dataTime;
	if (date == "") {
		benchmarkDialog.alertDialog(benchmarkLocale.field.date_placeholder);
		return false;
	}
	if (benchmarkObj.length==0) {
		benchmarkDialog.alertDialog(benchmarkLocale.field.indicator_placeholder);
		return false;
	}
	
	var dataList = [];
	for(var i = 0;i<benchmarkObj.length;i++){
		dataList.push(benchmarkObj[i].indicator);
	}
	var packageIds = "";
	for(var i = 0;i<packageObj.length;i++){
		if(i+1 == packageObj.length){
			packageIds += packageObj[i].id;
		}else{
			packageIds += packageObj[i].id +",";
		}
	}
	var benchmarkList = benchmarkAjax.sendAjax({
		url : requestPath + "/yddemo/benchmarks", // 请求url*
		params : {
			surround : "surround",
			date : date,
			dimension : benchmarkForm.getQueryConditions().dimension,
			packageIds : packageIds
		},
		type : "POST", // 请求类型*
		data : dataList // 请求参数*
	});
	benchmarkTable.initTable({
		divId : "benchmarkTableDiv", // 1.归属div*
		isScroll : true,
		datas : {
			dataList : benchmarkList
		},
		columns : BenchmarkSurroundProperties_View.getViewColumns()// 3.表格列配置
	});
	benchmarkTable.reloadData();
	initBar(benchmarkList);
	resetTableHeight("benchmarkTableDiv", array);
}

function initBar(benchmarkList){
	var text_arr = [];
//	var designValues = [];
	var datas = [];
	var surroundDatas = [];
	var yearOnYearDatas = [];
	for(var i = 0;i<benchmarkList.length;i++){
		text_arr.push(benchmarkList[i].companyName+benchmarkList[i].unitName+benchmarkList[i].indicator.name);
		if(benchmarkList[i].data==""){
			datas.push(0);
		}else{
			datas.push(parseFloat(benchmarkList[i].data));
		}
		/*if(benchmarkList[i].designValue==""){
			designValues.push(0);
		}else{
			designValues.push(parseFloat(benchmarkList[i].designValue));
		}*/
		if(benchmarkList[i].yearOnYearData==""){
			yearOnYearDatas.push(0);
		}else{
			yearOnYearDatas.push(parseFloat(benchmarkList[i].yearOnYearData));
		}
		if(benchmarkList[i].surroundData==""){
			surroundDatas.push(0);
		}else{
			surroundDatas.push(parseFloat(benchmarkList[i].surroundData));
		}
	}
	benchmarkColumn.initColumn("benchmarkChartDiv", {
		title : "",
		exporting : false,
		tooltip : {
			/*enabled : true,
			formatter : function() {
				return '<b>' + this.series.name+":"+this.point.y + '</b>';
			}*/
			headerFormat: '<span>{point.key}</span><br/>',
            pointFormat: '<span>{series.name}:{point.y}</span><br/>',
            useHTML: true
		},
		xAxis : {
			categories : text_arr,
			crosshair : true
		},
		yAxis : {
			min : 0,
			title : {
				text : benchmarkLocale.field.unit
			}
		},
		series : [ /*{
			name : benchmarkLocale.field.designValue,
			dataLabels : {
				enabled : true,
				format : '{y}'
			},
			data : designValues
		},*/{
			name : benchmarkLocale.field.value,
			dataLabels : {
				enabled : true,
				format : '{y}'
			},
			data : datas
		} ,{
			name : benchmarkLocale.field.yearOnYearValue,
			dataLabels : {
				enabled : true,
				format : '{y}'
			},
			data : yearOnYearDatas
		},{
			name : benchmarkLocale.field.surroundValue,
			dataLabels : {
				enabled : true,
				format : '{y}'
			},
			data : surroundDatas
		}]
	}, false);
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight("benchmarkTableDiv", array);
});
